package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.CoachMyVenueDto;
import com.woniu.model.dto.VenueCoachDto;
import com.woniu.model.dto.VenueLoadCoachDto;
import com.woniu.model.form.PageForm;
import com.woniu.model.form.PageVenueCoachForm;
import com.woniu.model.model.YVenueCoach;
import com.woniu.model.param.PageVenueCoachParam;
import com.woniu.model.param.VenueLoadCoachParam;
import com.woniu.service.YVenueCoachService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yVenueCoach")
@Api(tags = "教练与场馆的关系接口")
public class YVenueCoachController {

    @Resource
    private YVenueCoachService venueCoachService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 教练离职场馆
     * @param coachId 教练的id
     * @param venueId 场馆的id
     * @return
     */
    @GetMapping("/leaveVenueCoach")
    @ApiOperation(value = "教练离职场馆")
    public Result<?> leaveVenueCoach( Integer coachId,Integer venueId){
        Integer i = venueCoachService.leaveVenueCoach(coachId,venueId);
        if (!i.equals(1)){
            return Result.getResult().setMsg("离职失败");
        }
        return Result.getResult().setMsg("离职成功");
    }

    /**
     * 分页查询场馆的入驻教练
     * @param form 分页信息，场馆id，教练就职状态
     * @return
     */
    @GetMapping("/pageVenueCoach")
    @ApiOperation(value = "分页查询场馆的入驻教练")
    public Result<Page> pageVenueCoach(PageVenueCoachForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        PageVenueCoachParam param = BeanUtil.toBean(form, PageVenueCoachParam.class);
        param.setVenueId(venueId);
        Page<YVenueCoach> i = venueCoachService.pageVenueCoach(param);

        return Result.newInstance(Page.class).setMsg("查询成功");
    }



    /**
     * 分页查询场馆里面已经入驻的教练
     * @param form 分页信息
     * @return
     */
    @GetMapping("/loadPageVenueCoach")
    @ApiOperation(value = "分页查询场馆里面已经入驻的教练")
    public Result<Page<VenueLoadCoachDto>> loadPageVenueCoach(PageForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        VenueLoadCoachParam param = BeanUtil.toBean(form, VenueLoadCoachParam.class);
        param.setVenueId(venueId);
        Page<VenueLoadCoachDto> page = venueCoachService.loadPageVenueCoach(param);

        return Result.newPageInstance(VenueLoadCoachDto.class).setMsg("查询成功").setData(page);
    }




    /**
     * 根据教练token去查询该教练所在的场馆详细信息
     * @param token
     * @return
     */
    @GetMapping("/loadCoachMyVenue")
    @ApiOperation("根据教练token去查询该教练所在的场馆详细信息")
    public Result<?> loadCoachMyVenue(@RequestHeader String token) {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        return Result.newInstance(CoachMyVenueDto.class).setCode(200).setMsg("查询成功").setData(venueCoachService.loadCoachMyVenue(coachId));
    }


    /**
     * 根据场馆ID去查询该场馆内所有的教练
     * @param venueId
     * @return
     */
    @GetMapping("/VenueByIdCoach")
    @ApiOperation("根据场馆Id去查询该场馆内所有的教练")
    public Result<List<VenueCoachDto>> VenueByIdCoach(Integer venueId) {
        return Result.newListInstance(VenueCoachDto.class).setCode(200).setMsg("查询成功").setData(venueCoachService.VenueByIdCoach(venueId));
    }







    /**
     * 根据场馆token去查询该场馆内所有的教练
     * @param token
     * @return
     */
    @GetMapping("/loadListVenueCoach")
    @ApiOperation("根据场馆token去查询该场馆内所有的教练")
    public Result<?> loadListVenueCoach(@RequestHeader String token) {
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        return Result.newListInstance(VenueCoachDto.class).setCode(200).setMsg("查询成功").setData(venueCoachService.loadListVenueCoach(venueId));
    }

    /**
     * 根据场馆id去查询该场馆内所有的教练
     * @return
     */
    @GetMapping("/loadVenueCoachByVenueId")
    @ApiOperation("根据场馆id去查询该场馆内所有的教练")
    public Result<?> loadVenueCoachByVenueId(Integer venueId) {
        return Result.newListInstance(VenueCoachDto.class).setCode(200).setMsg("查询成功").setData(venueCoachService.loadListVenueCoach(venueId));
    }


}

